package ui.adapter;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.wyz.troika_test.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import model.bean.DisplayData;

/**
 * Created by 10153 on 2018/5/8.
 */

public class MainRecyclerAdapter extends RecyclerView.Adapter<MainRecyclerAdapter.MyViewHolder>
        implements View.OnClickListener{
    
    private static final String TAG = "MainRecyclerAdapter";
    
    private int[] colorIDs = new int[]{
            R.color.color_0, R.color.color_1, R.color.color_2,
            R.color.color_3, R.color.color_4, R.color.color_5,
            R.color.color_6, R.color.color_7, R.color.color_8, R.color.color_9
    };
    
    private OnMainListClickListener mListener = null;
    private Context mContext;
    private DisplayData mData = null;
    
    public MainRecyclerAdapter(Context mContext) {
        this.mContext = mContext;
    }
    
    public MainRecyclerAdapter(Context context, DisplayData data) {
        this.mContext = context;
        this.mData = data;
    }
    
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(this.mContext)
                .inflate(R.layout.contain_recycler_main_2, parent, false);
        MyViewHolder holder = new MyViewHolder(itemView);
        itemView.setOnClickListener(this);
        return holder;
    }
    
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        String display = null;
        String unit = null;
        int iconID = 0;
        switch (position){
            case 0:
                //卡路里消耗
                unit = "卡路里";
                break;
            case 1:
                //体重
                unit = "公斤";
                break;
            case 2:
                //步数
                unit = "步";
                break;
            case 3:
                //bpm
                if(mData != null)
                    display = Double.toString(mData.getBpm());
                unit = "BPM";
                break;
            case 4:
                //行走时间
                unit = "小时";
                break;
            case 5:
                //跑步时间
                unit = "小时";
                break;
            case 6:
                //骑车时间
                unit = "小时";
                break;
            case 7:
                //游泳时间
                unit = "小时";
                break;
            case 8:
                //驾车时间
                unit = "小时";
                break;
            case 9:
                //睡眠时间
                unit = "小时";
            default:
                break;
        }
        holder.cv_bg
                .setCardBackgroundColor(mContext.getResources().getColor(this.colorIDs[position]));
        holder.cv_bg.setAlpha((float)0.7);
        holder.tv_unit.setText(unit);
        holder.itemView.setTag(position);
        if(display != null)
            holder.tv_display.setText(display);
//        if(iconID != 0)
//            holder.img_icon.setImageDrawable(mContext.getResources().getDrawable(iconID));
    }
    
    @Override
    public int getItemCount() {
        return 10;
    }
    
    @Override
    public void onClick(View v) {
        if(this.mListener != null){
            mListener.onItemClick((int)v.getTag());
        }
    }
    
    public void setListener(OnMainListClickListener listener){
        this.mListener = listener;
    }
    
    public void updateDate(DisplayData data){
        this.mData = data;
        this.notifyDataSetChanged();
        Log.d(TAG, "updateDate: complete!");
    }
    
    class MyViewHolder extends RecyclerView.ViewHolder{
        
        @BindView(R.id.rc_main_card) CardView cv_bg;
        @BindView(R.id.rc_main_title) TextView tv_title;
        @BindView(R.id.rc_main_display) TextView tv_display;
        @BindView(R.id.rc_main_unit) TextView tv_unit;
//        @BindView(R.id.rc_main_icon) ImageView img_icon;
        
        MyViewHolder(View itemView){
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
    
    public interface OnMainListClickListener {
        void onItemClick(int position);
    }
}
